﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using TransferPost.Beans;
using TransferPost.Componts;

namespace TransferPost.util
{
    /// <summary>
    /// 通用中间层
    /// add by zhzhh at 2020年4月4日
    /// </summary>
    public class PostContext
    {

        #region " SingleCtx "
        private static PostContext _instance;

        private PostContext() { }

        public static PostContext Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new PostContext();

                    _instance.InitFormList();
                }
                return _instance;
            }
        }
        #endregion

        #region " Global Param "

        public static string URL_SVNIGNORECFG = @"http://friendm.gitee.io/transferpost/";


        #endregion

        private Func<string, string> Func_OutMsg;

        public void InitPrintEnv(Func<string, string> func)
        {
            Func_OutMsg = func;
        }

        public void PrintMsg(string s)
        {
            Func_OutMsg(s);
        }

        public ILog MyLogger = LogManager.GetLogger(typeof(PostContext));

        public List<frmInfo> FormList = new List<frmInfo>();

        /// <summary>
        /// 获取所有的Form窗体
        /// </summary>
        /// <returns></returns>
        public void InitFormList()
        {
            FormList.Clear();

            //  2017年9月26日，ZHZHH 根据类增加头声明，动态加载
            var baseType = typeof(TransferPost.Componts.DockContentEx);

            Assembly assems = Assembly.GetAssembly(baseType);

            // 2017年9月26日-20点57分，ZHZHH 因为是2.0的FrameWork，所以linq表达式是不行的。
            // 只能采用一种很奇怪的方式。DataTable 去便于IPC送到Main程序之后的排序。JUST DO IT。
            Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();

            foreach (var t in assems.GetTypes())
            {
                if (t.BaseType == baseType)
                {
                    object[] objAttr = t.GetCustomAttributes(typeof(TransferPost.Componts.PostFormAttribute), true);

                    if (objAttr != null && objAttr.Length > 0)
                    {
                        PostFormAttribute attr = objAttr[0] as PostFormAttribute;

                        frmInfo info = new frmInfo();
                        info.frmName = attr.FormName;
                        info.frmNo = attr.FormNo;
                        info.frmType = t;

                        FormList.Add(info);
                    }
                }
            }
        }
    }
}
